/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-22 下午1:45 Created by dwzhang
 */
package com.jade.framework.base.util;

import org.apache.shiro.codec.Base64;

/**
 * DataURI的处理。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a>
 * @version 1.0 15-1-22 下午1:45
 */
public class DataURIUtils
{
    /**
     *
     * @param dataURI data URI数据
     * @return array[0] content type array[1] encoding array[2] content
     */
    public static String[] parseDataURI(String dataURI)
    {
        String[] array = new String[3];
        //
        //data:image/png,content
        int index = dataURI.indexOf(',');
        String type = dataURI.substring(0, index);
        int i = type.indexOf(';');
        if (i > 0) {
            array[0] = type.substring(5, i);
            array[1] = type.substring(i + 1, index);
        }
        else {
            array[0] = type.substring(5, index);
            array[1] = null;
        }
        array[2] = dataURI.substring(index + 1).trim();
        return array;
    }

    public static byte[] parseContent(String encoding, String encodedContent)
    {
        if (StringUtilsEx.equalsIgnoreCase(encoding, "base64")) {
            return Base64.decode(encodedContent);
        }
        else {
            return encodedContent.getBytes();
        }
    }
}
